import {ref,reactive,nextTick} from 'vue'
import { EditType, Title } from '@/type/BaseEnum';
import { recordGetRecordApi, getImageUrlApi,hazardDetailApi} from '@/api/ReviewBusiness/01Project/project'
import { getArrayToHis,getArrayToHis1 } from '@/utils/index'
import { quoteStore } from '@/store/quoteData';
export default function abnormalDetails(RefShowImage?){
  // 异常详情的数据
  const RefAbnormal=ref<{show:(type:string,row?:any,detail?:any)=>void}>()
  const store = quoteStore()
  const showDate = reactive({
    // 数据怎么处理 1 查询数据 2 canvas数据 3 mark数据 4 analysis 
    fileDate: {},
    search: {
      filesId: '',
      pageNumber: 1
    },
    limits: true,
    // img数据
    recordList: [],
    imageId: '',
    img_info: [] as any,
    numPages: 0,
    type: 'R'
  })
  const lookDetail=async(row)=>{
    const res = await hazardDetailApi({id:row.id})
    if(res.code===200){
      RefAbnormal.value?.show(EditType.ADD,row,res.data)
    }    
  }
    const clearShowDate=()=>{
    showDate.fileDate={}
    showDate.search={
      filesId: '',
      pageNumber: 1
    }
    showDate.limits=true
    showDate.imageId=''
    showDate.img_info= [] as any
    showDate.numPages=0
    showDate.type='R'
    showDate.numPages=0
  }
    // 查询图片数据
  const getImage = async (row) => {
    console.log(row, 'fileDate');
    const res: any = await recordGetRecordApi(row)
    
    if (res.code == 200 && res.data.length > 0) {
      //数据
      console.log(res, '有数据');
      showDate.recordList = res.data
      showDate.numPages = res.data[0].count
      const imagePath = res.data[0].imagePath
      showDate.imageId = res.data[0].imageId
      const path = imagePath.substring(8, imagePath.length)
      const file = await getImageUrl(path) as any
      const image = window.webkitURL.createObjectURL(new Blob([file], { type: 'image/png' }))
      const arr = getArrayToHis(res.data)
      const item = {
        imageBase64: `${image}`,
        page: showDate.search.pageNumber,
        fileId: showDate.search.filesId,
        commentsDTOs: arr,
        records: res.data
      }
      // 这个地方有问题
      showDate.img_info.push({ ...item })
    } else {
      console.log('没有数据');
      // 没有数据 全部清空啊数据
      showDate.recordList = []
      showDate.numPages = 0
      showDate.imageId = ''
      showDate.img_info = []
    }
  }
  // 数据回显
  const getImageLook = async (row,detail:any) => {
    console.log(row, 'fileDate');
    const res: any = await recordGetRecordApi(row)
    if (res.code == 200 && res.data.length > 0) {
      //数据
      console.log(res, '有数据');
      showDate.recordList = res.data
      showDate.numPages = store.resetData.length
      const imagePath = res.data[0].imagePath
      showDate.imageId = res.data[0].imageId
      const path = imagePath.substring(8, imagePath.length)
      const file = await getImageUrl(path) as any
      const image = window.webkitURL.createObjectURL(new Blob([file], { type: 'image/png' }))
      const arr = getArrayToHis1(detail)
      const item = {
        imageBase64: `${image}`,
        page: showDate.search.pageNumber,
        fileId: showDate.search.filesId,
        commentsDTOs: arr,
        records: res.data
      }
      // 这个地方有问题
      showDate.img_info.push({ ...item })
    } else {
      console.log('没有数据');
      // 没有数据 全部清空啊数据
      showDate.recordList = []
      showDate.numPages = 0
      showDate.imageId = ''
      showDate.img_info = []
    }
  }
  // 本地数据 
  const getLocalData=async (row)=>{
    // 干嘛 本地数据
  }

   const getImageUrl = async (path) => {
    return new Promise((resolve, reject) => {
      getImageUrlApi(path).then((res) => {
        resolve(res)
      }).catch((err) => {
        console.log(err);
        reject(err)
      })
    })
  }
    const getFileList = async (row?) => {
    showDate.fileDate = row
    showDate.search.filesId = row.id
    showDate.search.pageNumber = 1
  }
    // 分页处理
  const btnPre = async () => {
    showDate.img_info = []
    RefShowImage.value?.clearCanvasDate()
    // 判断页码 上一页 
    // await getImage({ filesId: showDate.search.filesId, pageNumber: showDate.search.pageNumber - 1 })
    await getImageLook({filesId: store.resetData[showDate.search.pageNumber-2].filesId, pageNumber: store.resetData[showDate.search.pageNumber-2].pageNumber},store.resetData[showDate.search.pageNumber-2].distance)

    let page = showDate.search.pageNumber
    page = page > 1 ? page - 1 : showDate.numPages
    showDate.search.pageNumber = page
    showDate.imageId = showDate.img_info[0].records[0].imageId
    if (showDate.img_info.length > 0) {
      nextTick(() => {
        RefShowImage.value?.initImage()
      })
    }
  }
  const btnNext = async () => {
    console.log('next');
    
    showDate.img_info = []
    RefShowImage.value?.clearCanvasDate()
    await getImageLook({filesId: store.resetData[showDate.search.pageNumber].filesId, pageNumber: store.resetData[showDate.search.pageNumber].pageNumber},store.resetData[showDate.search.pageNumber].distance)
    let page = showDate.search.pageNumber
    page = page < showDate.numPages ? page + 1 : 1
    showDate.search.pageNumber = page
    showDate.imageId = showDate.img_info[0].records[0].imageId
    if (showDate.img_info.length > 0) {
      nextTick(() => {
        RefShowImage.value?.initImage()
      })
    }
  }
  return{
    RefAbnormal,lookDetail,showDate,clearShowDate,getImage,getFileList,btnPre,btnNext,getImageLook
  }
}